//
//  KHHStoryDetailTableViewCell.m
//  Travel
//
//  Created by dllo on 15/11/30.
//  Copyright © 2015年 doll. All rights reserved.
//

#import "KHHStoryDetailTableViewCell.h"
#import "UIImageView+WebCache.h"


@interface KHHStoryDetailTableViewCell ()

@property (nonatomic, retain)UIImageView *img;
@property (nonatomic, retain)UILabel *label;

@end

@implementation KHHStoryDetailTableViewCell

//初始化
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self createView];
    }
    return self;
}

- (void)createView
{
    self.img = [[UIImageView alloc]init];
    [self.contentView addSubview:self.img];

    self.label = [[UILabel alloc]init];
    self.label.font = [UIFont systemFontOfSize:15];
    self.label.textColor = [UIColor darkGrayColor];
    [self.contentView addSubview:self.label];

}

//布局
- (void)layoutSubviews
{
    
    [super layoutSubviews];
//    //夜间模式
//    if ([[NSUserDefaults standardUserDefaults]boolForKey:@"isNight"]) {
//        self.contentView.backgroundColor = [UIColor colorWithRed:0.28 green:0.28 blue:0.28 alpha:1];
//        self.label.textColor = [UIColor whiteColor];
//    }else
//    {
//        self.contentView.backgroundColor = [UIColor colorWithRed:0.98 green:0.97 blue:0.93 alpha:1];
//    }
    
    self.img.frame = CGRectMake(20, 20, HHWIDTH - 40, (self.detailModel.photo_height).integerValue  * HHWIDTH / (self.detailModel.photo_width).integerValue );
    
    
    CGFloat hLabel = [[self class] heightWithText:self.label.text fontSize:15 Width:HHWIDTH - 40];
    self.label.frame = CGRectMake(self.img.frame.origin.x, self.img.frame.origin.y + 20 + self.img.frame.size.height , HHWIDTH - 40, hLabel);
    self.label.numberOfLines = 0;
}

- (void)setDetailModel:(KHHRecommendModel *)detailModel
{
    _detailModel = detailModel;
    [self.img sd_setImageWithURL:[NSURL URLWithString:detailModel.photo] placeholderImage:nil];
    self.label.text = detailModel.text;
    
}

//计算文本高度的方法
+ (CGFloat)heightWithText:(NSString *)text fontSize:(CGFloat)fontsize Width:(CGFloat)width

{
    //设置一个字典 保存文本属性
    NSDictionary *dic = @{NSFontAttributeName:[UIFont systemFontOfSize:fontsize]};
    
    //预设尺寸 文本最大不会超过这个范围
    CGSize size = CGSizeMake(width, 2000);
    //根据文本信息 计算得到一个CGRect
    //参数1. 尺寸范围
    //参数2. 获取文本尺寸的方式
    //参数3. 文本属性
    //参数4. 无用信息(上下文)
    CGRect rect = [text boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    return rect.size.height;
}

//容错
- (void)setUserDic:(NSMutableDictionary *)userDic
{
    
}





- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
